<template>
  <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="150px">
      <div class="tip">节目表配置</div>
      <el-form-item label="id" prop="id" v-if="dataForm.id" hidden>
        <el-input v-model="dataForm.id" placeholder="id" disabled ></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="物理库" prop="databaseId">
            <el-select
              v-model.trim="dataForm.databaseId"
              filterable
              @change="selectByDatabaseIds($event)"
              placeholder="请选择物理库"
              style="width:100%"
            >
              <el-option
                v-for="database in databaseOptions"
                :key="database.id"
                :label="database.database.databaseName+'_'+database.schemaNameCn"
                :value="database.id"
              ></el-option>
            </el-select>
          </el-form-item>

        </el-col>
        <el-col :span="12">
          <el-form-item label="资料名称" prop="ddataClassId">

            <el-select
              v-model.trim="dataForm.ddataClassId"
              filterable
              @change="getTable($event)"
              placeholder="请选择资料"
              style="width:100%"
            >
              <el-option
                v-for="dataClass in dataClassIdOptions"
                :key="dataClass.DATA_CLASS_ID"
                :label="dataClass.CLASS_NAME"
                :value="dataClass.DATA_CLASS_ID"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
       <!-- <el-col :span="12">
          <el-form-item label="资料名称" prop="dataName">
            <el-input v-model="dataForm.dataName" placeholder="资料名称"></el-input>
          </el-form-item>
        </el-col>-->
        <el-col :span="12">
          <el-form-item label="表名" prop="tableName">
            <el-input v-model="dataForm.tableName" placeholder="表名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="四级编码" prop="ddataId">
            <el-input v-model="dataForm.ddataId" placeholder="四级编码"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="及时时间" prop="timely">
            <el-input v-model="dataForm.timely" placeholder="请输入及时时间"></el-input>
          </el-form-item>
        </el-col>
      <el-col :span="12">
          <el-form-item label="最小应收数" prop="minFileNum">
            <el-input v-model="dataForm.minFileNum" placeholder="最小应收数"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="资料时次" prop="jobCron">
            <el-input v-model="dataForm.jobCron" placeholder="资料时次"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否应收一致" prop="isFit">
            <el-radio-group v-model="dataForm.isFit">
              <el-radio  :label=0>一致</el-radio>
              <el-radio  :label=1>不一致</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="2">
        </el-col>
        <el-col :span="20">
       <!-- <div>
          <el-button size="mini" @click="handleAdd()" type="primary">Add</el-button>
          <el-table
            ref="mytable"
            :data="dataForm.recs"
            style="width: 100%"
          >
            <el-table-column
              align="center"
              v-for="(item,index,key) in table_columns"
              :item="item"
              :key="key"
              :index="index"
              :label="item.label"
            >
              <template slot-scope="scope">
                <el-input
                  v-if=" scope.row.edit"
                  size="small"
                  v-model="scope.row[item.prop]"
                  :placeholder="'请输入'+item.label"
                ></el-input>
                <span v-if="  !scope.row.edit">{{scope.row[item.prop]}}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                &lt;!&ndash; 全局控制的编辑 &ndash;&gt;
                <div v-if="is_edit&&scope.row.add==undefined" style="display: inline-block;">
                  &lt;!&ndash; 编辑 &ndash;&gt;
                  <el-button
                    size="mini"
                    v-if="!scope.row.edit"
                    @click="handleEdit(scope.$index, scope.row)"
                    type="primary"
                  >Edit</el-button>
                  &lt;!&ndash; 保存 &ndash;&gt;
                  <el-button
                    size="mini"
                    type="success"
                    :plain="true"
                    v-if="scope.row.edit"
                    @click="handleSave(scope.$index, scope.row)"
                  >Save</el-button>
                </div>
                &lt;!&ndash; 添加控制 &ndash;&gt;
                <div v-if="scope.row.add!=undefined&&scope.row.add" style="display: inline-block;">
                  &lt;!&ndash; 保存 &ndash;&gt;
                  <el-button
                    size="mini"
                    type="success"
                    :plain="true"
                    v-if="scope.row.edit"
                    @click="handleSave(scope.$index, scope.row)"
                  >Save</el-button>
                </div>
                &lt;!&ndash; 全局控制删除 &ndash;&gt;
                <el-button
                  size="mini"
                  v-if="is_delete&&scope.row.add==undefined"
                  :plain="true"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                >Delete</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>-->
          <el-button size="mini" @click="handleAdd()" type="primary">Add</el-button>
          <el-table :data="dataForm.recs" class="tb-edit" style="width: 100%" highlight-current-row @row-click="handleCurrentChange">
            <el-table-column label="时次" >
              <template scope="scope">
                <el-time-picker
                  v-model="scope.row.timeLevel"
                  :picker-options="{
                      selectableRange: '00:00:00 - 23:59:59'
                    }"
                  value-format="HH:mm:ss"
                  placeholder="任意时间点">
                </el-time-picker>
                <span>{{scope.row.timeLevel}}</span>
              </template>
            </el-table-column>
            <el-table-column label="应收" >
              <template scope="scope">
                <el-input size="small" v-model="scope.row.minFileNum" placeholder="请输入内容" @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.minFileNum}}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template scope="scope">
                <!--<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
                <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>

      </el-row>


    </el-form>
</template>
<style>
  .tip {
    padding: 8px 16px;
    background-color: #ecf8ff;
    border-radius: 4px;
    border-left: 5px solid #50bfff;
    margin: 20px 0;
  }
  .tb-edit .el-input {
    display: none
  }
  .tb-edit .current-row .el-input {
    display: block
  }
  .tb-edit .current-row .el-input+span {
    display: none
  }
</style>
<script>
  import {getObj, addObj, putObj,findAllDataBase,getByDatabaseId,getByDatabaseIdAndClassId} from '@/api/job/playBill'

  export default {
    data() {
      return {
        databaseOptions: [],
        dataClassIdOptions:[],
        dataForm: {
          id:'',
          databaseId:'',
          ddataId: '',
          ddataClassId: '',
          dataName: '',
          timely : '',
          minFileNum: '',
          isFit: 0,
          jobCron:'',
          triggerStatus: 1,
          recs: [],
        },
        dataRule: {

        },
        table_columns: [
          {
            prop: "timeLevel",
            label: "时间",
            width: "150"
          },
          {
            prop: "minFileNum",
            label: "最小应收",
            width: "150"
          }
        ],
        new_date_json: {}, //数据结构
        multipleSelection: [], //复选框，数据
        is_edit: true, //是否可编辑
        is_delete: true, //是否可删除
        space_color: true, //隔行变色

      }
    },
    created() {
      this.getDataBase();
    },
    methods: {
      init(id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (id) {
            getObj(id).then(response => {
              this.dataForm = response.data.data;
              for (var key in this.dataForm.recs) {
                this.dataForm.recs[key].edit = true;
              }
              this.selectByDatabaseIds(this.dataForm.databaseId)
            })
          }
        })
      },
      getDataBase(){
        findAllDataBase().then(response => {
          this.databaseOptions = this.unique(response.data.data);
        })
      },
      unique(arr) {
        const res = new Map();
        return arr.filter((arr) => !res.has(arr.database.id) && res.set(arr.database.id, 1)&&arr.database.id=='FIDB')
      },
      selectByDatabaseIds(databaseId) {
         getByDatabaseId(databaseId).then(response => {
          this.dataClassIdOptions = response.data.data;
        });
      },
      getTable(dataClassId) {
        this.dataForm.dataName=this.changeLocationValue(dataClassId);
        getByDatabaseIdAndClassId(this.dataForm.databaseId, dataClassId).then(response => {
          this.dataForm.ddataId = response.data.data.ddataId;
          this.dataForm.tableName = response.data.data.tableName;
        });
      },
      changeLocationValue(val){
        //locations是v-for里面的也是datas里面的值
        let obj = {};
        obj = this.dataClassIdOptions.find((item)=>{
          return item.DATA_CLASS_ID === val;
        });
        let name = ''
        name = obj.CLASS_NAME;
        return name
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                this.disPlayBillvisible()
                this.$emit('refreshPlayBillData')
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.disPlayBillvisible()
                this.$emit('refreshPlayBillData', data.data.data)
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      },
      disPlayBillvisible () {
        this.$emit('disPlayBillvisible')
      },
      //重置表单
      closeDialog() {
        this.$refs["dataForm"].resetFields()
      },
      handleCurrentChange(row, event, column) {
        //console.log(row, event, column, event.currentTarget)
      },
      handleAdd() {
        var addDataJson = {};
        for (var key in this.new_date_json) {
          if (key === "edit") {
            delete addDataJson[key];
          } else if (key === "add") {
            delete addDataJson[key];
          } else {
            addDataJson[key] = "";
          }
        }
        addDataJson.edit = true;
        addDataJson.add = true;
        this.dataForm.recs.push(addDataJson);
      },
      //编辑
      handleEdit(index, row) {
        //row.edit = true;
      },
      //删除
      handleDelete(index, row) {

        this.dataForm.recs.splice(index, 1);

        this.$message({
          message: "删除成功！",
          type: "success"
        });
      },
      //保存
      /*handleSave(index, row) {
        row.edit = false;

        delete this.dataForm.recs[index].add;

        this.$message({
          message: "保存成功！",
          type: "success"
        });
      }*/


    }
  }
</script>

